<?php
namespace express;


class AliKdSearch{
    /**
     * 快递查询
     * @param string  $kdCode 快递单号
     * @param string  $kdType  快递类型(默认为null)
     * 
     * @return json
     */
    static public function kdSearch($kdCode, $kdType = null){
        $host = "https://wuliu.market.alicloudapi.com";//api访问链接
        $path = "/kdi";//API访问后缀
        $method = "GET";
        $appcode = "7149c8b812db4310a06ced74026cacbe"; //替换成自己的阿里云appcode
        $headers = ["Authorization:APPCODE " . $appcode];
        $querys = "no=$kdCode&type=$kdType";  //参数写在这里
        $url = $host . $path . "?" . $querys; //url拼接
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        //curl_setopt($curl, CURLOPT_HEADER, true); 如不输出json, 请打开这行代码，打印调试头部状态码。
        //状态码: 200 正常；400 URL无效；401 appCode错误； 403 次数用完； 500 API网管错误
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        return curl_exec($curl);
    }

}